package com.cy.study.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cy.study.Enum.PlanSituationEnum;
import com.cy.study.Enum.PlanStatusEnum;
import com.cy.study.common.BasePageReq;
import com.cy.study.entity.UserPlanEntity;
import com.cy.study.exception.BusinessException;
import com.cy.study.mapper.UserPlanMapper;
import com.cy.study.req.PlanAddReq;
import com.cy.study.req.PlanDeleteReq;
import com.cy.study.req.PlanUpdateReq;
import com.cy.study.res.PlanListRes;
import com.cy.study.service.UserPlanService;
import com.cy.study.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class UserPlanServiceImpl extends ServiceImpl<UserPlanMapper, UserPlanEntity> implements UserPlanService {
    @Override
    public Integer addPlan(PlanAddReq req) {
        if(req.getEndTime()==null){
            throw new BusinessException("计划结束时间不能为空");
        }
        LocalDateTime transTime = req.getEndTime().withHour(23).withMinute(59).withSecond(59);
        if(req.getEndTime().isBefore(LocalDateTime.now())){
            throw new BusinessException("结束时间不能早于开始时间");
        }
        Integer userId = SecurityUtils.getUserId();
        UserPlanEntity plan = new UserPlanEntity();
        plan.setUserId(userId);
        plan.setPlanName(req.getPlanName());
        plan.setCreateTime(LocalDateTime.now());
        plan.setPlanTask(req.getPlanTask());
        plan.setEndTime(transTime);
        plan.setSituation(PlanSituationEnum.NORMAL.getValue());
        plan.setCurrentFinishPercent(0);
        plan.setStatus(PlanStatusEnum.ING.getValue());
        this.save(plan);
        return plan.getId();
    }

    @Override
    public void deletePlan(PlanDeleteReq req) {
        removeById(req.getId());
    }

    @Override
    public void updatePlan(PlanUpdateReq req) {
        if(req.getCurrentFinishPercent()==null){
            return;
        }
        if(req.getCurrentFinishPercent()>100||req.getCurrentFinishPercent()<0){
            throw new BusinessException("计划完成百分比只能设置为0-100");
        }
        UserPlanEntity planEntity = this.getBaseMapper().selectById(req.getId());
        planEntity.setCurrentFinishPercent(req.getCurrentFinishPercent());
        if(req.getCurrentFinishPercent()==100){
            planEntity.setFinishTime(LocalDateTime.now());
            planEntity.setStatus(PlanStatusEnum.FINISHED.getValue());
        }
        planEntity.setUpdateTime(LocalDateTime.now());
        this.saveOrUpdate(planEntity);
    }

    @Override
    public Page<PlanListRes> planList(BasePageReq req) {
        Integer userId = SecurityUtils.getUserId();
        Page page = new Page(req.getPageNum(),req.getPageSize());
        QueryWrapper<UserPlanEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(UserPlanEntity::getUserId,userId).
                orderByDesc(UserPlanEntity::getStatus,UserPlanEntity::getCreateTime);
        Page info = this.getBaseMapper().selectPage(page, wrapper);
        List<UserPlanEntity> record = info.getRecords();
        List<PlanListRes> res = new ArrayList<>();
        record.stream().forEach(e->{
            PlanListRes temp = new PlanListRes();
            BeanUtils.copyProperties(e,temp);
            res.add(temp);
        });
        info.setRecords(res);
        return info;
    }
}
